package com.vil.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.utils.StringUtils;
import com.vil.common.JedisUtils;
import com.vil.entity.Admin;
import com.vil.entity.Authority;
import com.vil.entity.Function;
import com.vil.entity.Menu;
import com.vil.service.AdminService;
import com.vil.service.FunctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("index")
public class IndexController extends BaseController{


    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private FunctionService functionService;

    @Autowired
    private AdminService adminService;


    //去首页
    @RequestMapping("index.html")
    public String toIndex(Model model){
        Admin currentAdmin = this.getCurrentAdmin();
        Admin admin = new Admin();
        admin.setLogin_count(currentAdmin.getLogin_count()+1);
        admin.setId(currentAdmin.getId());
        adminService.updateByPrimaryKeySelective(admin);
        model.addAttribute("admin",currentAdmin);
//        model.addAttribute("url","/index/welcome.html");
        return "admin/welcome";
    }

//    //加载欢迎页
//    @RequestMapping("welcome.html")
//    public String withWelcome(Model model){
//        Admin currentAdmin = this.getCurrentAdmin();
//        Admin admin = new Admin();
//        admin.setLogin_count(currentAdmin.getLogin_count()+1);
//        admin.setId(currentAdmin.getId());
//        adminService.updateByPrimaryKeySelective(admin);
//        model.addAttribute("admin",admin);
//        return "admin/welcome";
//    }

    @PostMapping("menus")
    @ResponseBody
    public String menus(Model model){
//        System.out.println("进来了");
        //1-获取当前用户
        Admin admin=this.getCurrentAdmin();
//        System.out.println(admin.getRole_id()+"角色id");
        if(null !=admin){
            if (!JedisUtils.exist("vlists"+admin.getRole_id())) {
                List<Menu> vlist=getCurrentUserFunctionMenuList(admin.getRole_id());
                String vidStr = JSONObject.toJSONString(vlist);
                JedisUtils.set("vlists"+admin.getRole_id(),vidStr);
//                redisTemplate.opsForValue().set("vilmenu"+admin.getRole_id(),vidStr);
//				JSONArray objects = JSONArray.parseArray(midStr);

                return vidStr;
            }else {
                String rString = JedisUtils.get("vlists"+admin.getRole_id());
                System.out.println("redis:"+rString);
                JSONArray objects = JSONArray.parseArray(rString);
                if (rString!=null&&!"".equals(rString)) {
//                    model.addAttribute("admin",admin);
                    return rString;
                }else {
                    return "redirect:login/login.html";//重定向到根目录-首页
                }
            }
        }
        return "redirect:login/login.html";
    }

    //2-根据当前用户角色获得当前用户的菜单列表
    protected List<Menu> getCurrentUserFunctionMenuList(Long roleId) {
        List<Menu> mlist=new ArrayList<Menu>();
        Authority auth = new Authority();
        auth.setRole_id(roleId);
        List<Function> f1list= functionService.getMainFunctionList(auth);
        for (Function fun: f1list) {
            Menu mu =new Menu(); //声明菜单对象
            mu.setMainMenu(fun);   //设置1级菜单
            fun.setRole_id(roleId);
            List<Function> f2list=functionService.getSubFunctionList(fun);

            mu.setSubMenus(f2list); //设置2级菜单
            mlist.add(mu); //装载在mlist中
//            System.out.println("获取菜单成功Q");
        }
        return mlist;
    }


}
